package bookez.model.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;

import bookez.model.businessobject.Route;
import bookez.model.dao.RouteDao;
import bookez.model.dao.generic.GenericDaoHibernateImpl;
import bookez.model.exception.DataAccessLayerException;
import bookez.model.exception.DuplicateValueException;

public class RouteDaoHibernateImpl extends GenericDaoHibernateImpl<Route, Long> implements RouteDao{
	
	/**
	 * Constructor
	 * @param type
	 */
	public RouteDaoHibernateImpl(Class<Route> type) {
		super(type);
		// TODO Auto-generated constructor stub
	}//end constructor
	
	@Override
	public Long create(Route object) throws DataAccessLayerException {
		// TODO Auto-generated method stub
		
		Session session = getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		
		Long id = null;
		
		try {
			id =  (Long)session.save( object );
		}//end try
		
		catch( ConstraintViolationException cve ) {
			tx.rollback();
			session.close();
			throw new DuplicateValueException( cve );
		}
		
		catch ( Exception ex ) {
			tx.rollback();
			session.close();
			throw new DataAccessLayerException( ex );
		}
		tx.commit();
		return id;
	}//end method create

	@SuppressWarnings("unchecked")
	@Override
	public List<Route> findByFromCity(String fromCity)
			throws DataAccessLayerException {
		// TODO Auto-generated method stub
		List<Route> result = null;
		
		try {
			Query query = getSession().getNamedQuery( Route.NamedQueries.FIND_BY_FROM_CITY );
			query.setParameter( "city", fromCity );
			
			result  = query.list();
		}//end try

		catch ( Exception ex ) {
			throw new DataAccessLayerException( ex );
		}//end catch
		return result;
	}//end method findByFromCity

	@SuppressWarnings("unchecked")
	@Override
	public List<Route> findByToCity(String toCity)
			throws DataAccessLayerException {
		// TODO Auto-generated method stub
		List<Route> result = null;
		
		try {
			Query query = getSession().getNamedQuery( Route.NamedQueries.FIND_BY_TO_CITY );
			query.setParameter( "city", toCity );
			
			result = query.list();
		}//end try

		catch ( Exception ex ) {
			throw new DataAccessLayerException( ex );
		}//end catch		
		return result;
	}//end method findByToCity

	@Override
	public Route findByCities(String fromCity, String toCity)
			throws DataAccessLayerException {
		// TODO Auto-generated method stub
		Route result = null;
		
		try {
			Query query = getSession().getNamedQuery( Route.NamedQueries.FIND_BY_CITIES );
			query.setParameter( "fromCity", fromCity );
			query.setParameter( "toCity", toCity );
			
			result = (Route) query.uniqueResult();
		}//end try

		catch ( Exception ex ) {
			throw new DataAccessLayerException( ex );
		}//end catch
		
		return result;
	}//end method findByCities

	@SuppressWarnings("unchecked")
	@Override
	public List<Route> findAll() throws DataAccessLayerException {
		// TODO Auto-generated method stub
		List<Route> result = null;
		
		try {
			Query query = getSession().getNamedQuery( Route.NamedQueries.FIND_ALL );
			
			result = query.list();
		}//end try

		catch ( Exception ex ) {
			throw new DataAccessLayerException( ex );
		}//end catch		
		return result;
	}//end method findAll

}//end class RouteDaoHibernateImpl
